package com.hengda.smart.speak.d.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.hengda.smart.speak.d.R;
import com.hengda.smart.speak.d.dialog.anim.BaseEffects;
import com.hengda.smart.speak.d.dialog.anim.Effectstype;


/**
 *
 * @Description 设置项弹出框
 * @author wzq
 * @date 2015-7-4 上午9:20:55
 * @update (date)
 * @version V1.0
 */
public class HDMediaBuilder extends Dialog implements DialogInterface,android.view.View.OnClickListener {

    private TextView mediaNameTv;//音频文件名称
    private TextView mediaDurationTv;//文件总长度
    private TextView mediaPlayTv;//已经播放长度
    private Button dialogCloseBtn;//关闭弹框按钮
    private Button playBtn;//播放暂停按钮
    private SeekBar mediaSeekBar;    //音频进度seekbar

    private LinearLayout dialogMediaLL;



    //  内容view
    private View mDialogView;

    private Effectstype type=null;
    private int mDuration = -1;

    private static  int mOrientation=1;

    private boolean isCancelable=true;

    private volatile static HDMediaBuilder instance;
    private Context context;


    public HDMediaBuilder(Context context) {
        super(context);
        init(context);

    }

    public HDMediaBuilder(Context context, int theme) {
        super(context, theme);
        init(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowManager.LayoutParams params = getWindow().getAttributes();
//        params.width  = 540;
        params.width  = ViewGroup.LayoutParams.MATCH_PARENT;
//        params.height = 650;
        params.height = ViewGroup.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);

    }

    public static HDMediaBuilder getInstance(Context context) {

        int ort=context.getResources().getConfiguration().orientation;
        if (mOrientation!=ort){
            mOrientation=ort;
            instance=null;
        }

        if (instance == null) {
            synchronized (HDMediaBuilder.class) {
                if (instance == null) {
                    instance = new HDMediaBuilder(context, R.style.dialog_untran);
                }
            }
        }
        return instance;
    }

    private void init(Context context) {
        mDialogView 	= View.inflate(context, R.layout.layout_dialog_media, null);
        mediaNameTv		=(TextView)mDialogView.findViewById(R.id.media_title_tv);
        mediaDurationTv =(TextView)mDialogView.findViewById(R.id.media_duration);
        mediaPlayTv		=(TextView)mDialogView.findViewById(R.id.media_has_played);
        dialogCloseBtn	=(Button)mDialogView.findViewById(R.id.media_clost_btn);
        playBtn			=(Button)mDialogView.findViewById(R.id.media_play);
        dialogMediaLL	=(LinearLayout)mDialogView.findViewById(R.id.lrc_ll);
        mediaSeekBar	=(SeekBar)mDialogView.findViewById(R.id.media_seekbar);

        setContentView(mDialogView);


        dialogMediaLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isCancelable)
                    dismiss();
            }
        });
        getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    }


    public HDMediaBuilder withTitle(CharSequence title) {
        mediaNameTv.setText(title);
        dialogCloseBtn.setOnClickListener(this);
        return this;
    }

    public HDMediaBuilder withTitleColor(String colorString) {
        mediaNameTv.setTextColor(Color.parseColor(colorString));
        return this;
    }



    public HDMediaBuilder withDuration(int duration) {
        this.mDuration=duration;
        return this;
    }

    public HDMediaBuilder withEffect(Effectstype type) {
        this.type=type;
        return this;
    }

    public HDMediaBuilder setOnSeekListenner(OnSeekBarChangeListener changeListenner){
        mediaSeekBar.setOnSeekBarChangeListener(changeListenner);
        return this;
    }

    public HDMediaBuilder setOnclickListenner(android.view.View.OnClickListener listenner){
        playBtn.setOnClickListener(listenner);
        return this;
    }


    public HDMediaBuilder isCancelableOnTouchOutside(boolean cancelable) {
        this.isCancelable=cancelable;
        this.setCanceledOnTouchOutside(cancelable);
        return this;
    }

    public HDMediaBuilder isCancelable(boolean cancelable) {
        this.isCancelable=cancelable;
        this.setCancelable(cancelable);
        return this;
    }


    public void setPlayBarBg(int bg){
        playBtn.setBackgroundResource(bg);
    }

    public void setSeekbarMax(int duration) {
        mediaSeekBar.setMax(duration);
    }
    public void setSeekPosition(int position){
        mediaSeekBar.setProgress(position);
    }
    public void setDuration(String duration){
        mediaDurationTv.setText(duration);
    }
    public void setHasPlay(String playedString){
        mediaPlayTv.setText(playedString);
    }

    @Override
    public void show() {
        super.show();
    }

    private void start(Effectstype type){
        BaseEffects animator = type.getAnimator();
        if(mDuration != -1){
            animator.setDuration(Math.abs(mDuration));
        }
        animator.start(dialogMediaLL);
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    @Override
    public void onClick(View v) {
        dismiss();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
